package chapter05.item28;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author zhaoyuqiang
 * @date 2021年10月27日 11:43
 * @description 第28条：列表优于数组
 */
public class Chooser<T> {
    private final List<T> choicesList;

    public Chooser(Collection<T> choicesList) {
        this.choicesList = new ArrayList<>(choicesList);
    }

    public T choose(){
        final ThreadLocalRandom current = ThreadLocalRandom.current();
        return choicesList.get(current.nextInt(choicesList.size()));
    }

    public static void main(String[] args) {
        final List<Integer> integerList = List.of(1, 2, 3, 4, 5);
        final Chooser<Integer> integerChooser = new Chooser<>(integerList);
        for (int i = 0; i < 10; i++) {
            final Integer choose = integerChooser.choose();
            System.out.println(choose);
        }
    }
}
